/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package matching.aligment;

import java.io.IOException;
import java.net.URI;
import java.net.URISyntaxException;
import java.util.Properties;
import java.util.logging.Level;
import java.util.logging.Logger;
import org.semanticweb.owl.align.Alignment;
import org.semanticweb.owl.align.AlignmentException;
import org.semanticweb.owl.align.AlignmentProcess;
import fr.inrialpes.exmo.align.impl.BasicParameters;
import fr.inrialpes.exmo.align.impl.method.StringDistAlignment;
import java.util.ArrayList;
import java.util.Iterator;
import matching.ClassMatch;
import matching.DataPropertyMatch;
import matching.Match;
import matching.Matching;
import matching.ObjectPropertyMatch;
import matching.triple.ClassTriple;
import matching.triple.DataPropertyTriple;
import matching.triple.ObjectPropertyTriple;
import ontology.Ontology;
import org.semanticweb.owl.align.Cell;
import org.semanticweb.owl.model.OWLClass;
import org.semanticweb.owl.model.OWLDataProperty;
import org.semanticweb.owl.model.OWLObjectProperty;
import org.semanticweb.owl.model.OWLOntologyCreationException;

public class Alinhar {


	public static void main(String[] args) throws URISyntaxException, AlignmentException, IOException, Exception {
		URI onto1 = new URI("file:///C:/Users/arida/Documents/NetBeansProjects/TesteAlig/Ontologies/Amazon.owl");
		URI onto2 = new URI("file:///C:/Users/arida/Documents/NetBeansProjects/TesteAlig/Ontologies/Ebay.owl");
		URI onto3 = new URI("file:///C:/Users/arida/Documents/NetBeansProjects/TesteAlig/Ontologies/Sales.owl");
        try {
           
            Ontology ontologia1 = new Ontology(onto1);

            Ontology ontologia2 = new Ontology(onto3);
            alinhamento(ontologia1, ontologia2, "arquivo1.xml");
            // alinhamento(onto2, onto3, "arquivo2.xml");
        } catch (OWLOntologyCreationException ex) {
            Logger.getLogger(Alinhar.class.getName()).log(Level.SEVERE, null, ex);
        }
		
	}

	public static Matching alinhamento(Ontology onto1, Ontology onto2, String arquivo) throws URISyntaxException, AlignmentException, IOException, OWLOntologyCreationException, Exception {
		Properties params = new BasicParameters();         
                
               /* OWLOntologyManager manager1 = OWLManager.createOWLOntologyManager();
                Ontology onto1 = new Ontology(manager1.createOntology(ontologia1));

                OWLOntologyManager manager2 = OWLManager.createOWLOntologyManager();
                Ontology onto2 = new Ontology(manager2.createOntology(ontologia2));*/

                //Matching retMat = new Matching(onto1 , onto2, onto1.owl.getURI().toString(), onto2.owl.getURI().toString());

                ArrayList<Match> matchingsClass = new ArrayList<Match>();
                ArrayList<Match> matchingsDataProp = new ArrayList<Match>();
                ArrayList<Match> matchingsObjProp = new ArrayList<Match>();
                
		AlignmentProcess a1 = new StringDistAlignment();
		params.setProperty("stringFunction","smoaDistance");
		a1.init ( onto1.uriLocation, onto2.uriLocation );
		a1.align( (Alignment)null, params );
		/*PrintWriter writer = new PrintWriter (
		                      new BufferedWriter(
		                       new FileWriter(arquivo)), true);*/
                Iterator it = a1.iterator();
                Cell c;
                String name1;
                String name2;
                OWLClass cl1;
                OWLClass cl2;
                OWLDataProperty  dt1;
                OWLDataProperty  dt2;
                OWLObjectProperty  ob1;
                OWLObjectProperty  ob2;


                while(it.hasNext()){
                    c = (Cell) it.next();
                    name1 = transformToName(c.getObject1().toString());
                    name2 = transformToName(c.getObject2().toString());

                    System.out.println(name1+" "+name2);
                    System.out.println("classe "+onto1.getClass(name1));

                    Object o1 = onto1.getResource(name1);
                    Object o2 = onto1.getResource(name2);


                    if(o1 instanceof OWLClass && o2 instanceof OWLClass){
                        matchingsClass.add(new ClassMatch(new ClassTriple((OWLClass)o1, null, onto1),
                                                          new ClassTriple((OWLClass)o2, null, onto2),
                                                          onto1, onto2));
                    }
                    else  if(o1 instanceof OWLDataProperty
                            && (dt2 = onto2.getDataProperty(name2))!= null){
                        matchingsDataProp.add(
                                new DataPropertyMatch(new DataPropertyTriple((OWLDataProperty) o1, null,null,onto1),
                                                      new DataPropertyTriple((OWLDataProperty) o2, null,null,onto2),
                                                      onto1, onto2));
                    } else {
                         ob1 = onto1.getObjectProperty(name1);
                         ob2 = onto2.getObjectProperty(name2);  
                         matchingsObjProp.add(
                                new ObjectPropertyMatch(new ObjectPropertyTriple(ob1, null,null,onto1),
                                                        new ObjectPropertyTriple(ob2, null,null,onto2),
                                                        onto1, onto2));
                    }
                }

                //retMat.print();
                
		/*AlignmentVisitor renderer = new OWLAxiomsRendererVisitor(writer);
		a1.render(renderer);
		writer.flush();
		writer.close();*/

		/*XMLOutputter xout = new XMLOutputter(Format.getPrettyFormat());
		FileWriter writer2 = new FileWriter("arquivo.xml");
		xout.output(documento, writer);
		writer.close();
		*/
                return null;
	}

        private static String transformToName(String asURI){
            return asURI.split("#")[1].replaceFirst(">", "");
        }

        private static void searchForContexts(){

        }

}

